<template>
  <div class="app-container">
    <el-collapse v-model="activeName" accordion>
      <el-collapse-item name="1">
        <template slot="title">
          <i class="el-icon-search" />搜索栏
        </template>
        <div class="head-container">
          <el-form class="searchform" :model="params" :inline="true" size="mini" label-width="auto" @keyup.enter.native="toQuery">
            <el-form-item label="主题">
              <el-input v-model="params.topic" placeholder="输入主题" clearable style="width: 160px;" />
            </el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" style="float:right;margin-left:0px;" icon="el-icon-refresh" round @click="resetForm">重置</el-button>
              <el-button size="mini" type="primary" style="float:right;margin-left:0px;" icon="el-icon-search" round @click="toQuery">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-item>
    </el-collapse>
    <div style="padding-top:5px">
      <el-button size="mini" type="danger" style="float:right;margin-bottom:5px;margin-right:0px;" icon="el-icon-remove" round @click="batchDelete">删除</el-button>
      <el-button size="mini" type="success" style="float:right;margin-bottom:5px;margin-right:0px;" round icon="el-icon-circle-plus" @click="toAdd">新增</el-button>
      <el-table
        ref="table"
        :data="data"
        :row-key="getRowKeys"
        stripe
        size="small"
        :height="tableHeight"
        border
        style="width: 100%;"
        @row-click="rowClick"
        @row-dblclick="rowDoubleClick"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
        @header-dragend="colChange"
      >
        <el-table-column :reserve-selection="true" type="selection" align="center" width="55" />
        <!--<el-table-column :show-overflow-tooltip="true" label="ID" prop="id" fixed="left" :width="colWidths.id" />-->
        <el-table-column :show-overflow-tooltip="true" label="主题" prop="topic" :width="colWidths.topic ? colWidths.topic : 150" />
        <el-table-column :show-overflow-tooltip="true" label="公告内容" prop="context" :width="colWidths.context ? colWidths.context : 150" />
        <el-table-column :show-overflow-tooltip="true" label="是否在主页显示" sortable prop="visible" :width="colWidths.visible ? colWidths.visible : 130">
          <template slot-scope="scope">
            <span>{{ showNameByValue(scope.row.visible, visibles) }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="创建人" sortable prop="createdBy" :width="colWidths.createdBy" />
        <el-table-column :show-overflow-tooltip="true" label="更新人" sortable prop="updatedBy" :width="colWidths.updatedBy" />
        <el-table-column :show-overflow-tooltip="true" label="创建时间" sortable prop="createdTime" :width="colWidths.createdTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" label="更新时间" sortable prop="updatedTime" :width="colWidths.updatedTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updatedTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="operation" align="center" fixed="right" width="185">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" icon="el-icon-edit" round @click="toEdit(scope.row)">修改</el-button>
            <el-button size="mini" type="warning" icon="el-icon-remove" round @click="toDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page.sync="currentPage"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <edit ref="edit" :parent="parent" />
  </div>
</template>

<script>
import initData from '@/utils/initData'
import { parseTime } from '@/utils/index'
import { batchDeleteNotice, deleteNotice } from '@/api/notice'
import edit from './edit'
import { Message } from 'element-ui'
import { showNameByValue } from '@/utils/selectUtil'

export default {
  components: { edit },
  mixins: [initData],
  data() {
    return {
      syncTime: [],
      ids: [],
      parent: this,
      activeName: '1',
      tableHeight: window.innerHeight * 0.58,
      visibles: [{ value: 1, name: '是' }, { value: 0, name: '否' }]
    }
  },
  watch: {
    data: {
      handler(n, o) {
        this.$nextTick(() => {
          this.doLayout()
        })
      }
    }
  },
  mounted() {
    window.addEventListener('resize', this.doLayout, true)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.doLayout, true)
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    showNameByValue,
    doLayout() {
      if (this.$refs.table) {
        this.$refs.table.doLayout()
      }
    },
    beforeInit() {
      this.url = '/notice/queryPage'
      this.params.pageNum = this.page
      this.params.pageSize = this.size
      return true
    },
    rowClick(row) {
      this.$refs.table.clearSelection(this.multipleSelection)
      this.$refs.table.toggleRowSelection(row, true)
    },
    rowDoubleClick(row) {
      this.toEditRule(row)
    },
    // 列表排序触发
    sortChange(val) {
      const sortCol = val.prop.replace(/([A-Z])/g, '_$1').toUpperCase()
      let sortType = ''
      if (val.order === 'descending') {
        sortType = ' DESC'
      } else if (val.order === 'ascending') {
        sortType = ' ASC'
      }
      this.params.sort = sortCol + sortType
      this.init()
    },
    // 查询
    toQuery() {
      this.page = 0
      this.currentPage = 1
      this.init()
    },
    // 新增规则
    toAdd() {
      const edit = this.$refs.edit
      edit.title = '新增公告'
      edit.visibles = this.visibles
      edit.dialog = true
    },
    // 编辑规则
    toEdit(notice) {
      this.$refs.table.clearSelection(this.multipleSelection)
      this.$refs.table.toggleRowSelection(notice, true)
      const edit = this.$refs.edit
      var json = JSON.stringify(notice)
      var copyCotice = JSON.parse(json)
      edit.title = '编辑公告'
      edit.visibles = this.visibles
      edit.notice = copyCotice
      edit.dialog = true
    },
    // 查询栏重置触发
    resetForm() {
      this.params = { }
    },
    // 列表多选框触发
    handleSelectionChange(val) {
      this.ids = []
      val.forEach(item => {
        this.ids.push(item.id)
      })
    },
    // 翻页选择row的key值
    getRowKeys(row) {
      return row.id
    },
    showSucessMessage(msg) {
      Message({
        message: msg,
        type: 'success',
        duration: 2 * 1000
      })
    },
    showWarningMessage(msg) {
      Message({
        message: msg,
        type: 'warning',
        duration: 2 * 1000
      })
    },
    toDelete(notice) {
      this.$refs.table.clearSelection(this.multipleSelection)
      this.$refs.table.toggleRowSelection(notice, true)
      this.$confirm('是否删除?', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning',
        roundButton: true
      }).then(() => {
        deleteNotice(notice.id).then(res => {
          if (res) {
            this.showSucessMessage(res.result)
            this.toQuery()
          }
        })
      })
    },
    // 批量删除
    batchDelete() {
      if (this.ids.length <= 0) {
        this.showWarningMessage('请选择删除项!')
        return
      }
      this.$confirm('是否删除?', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning',
        roundButton: true
      }).then(() => {
        batchDeleteNotice(this.ids).then(res => {
          if (res) {
            this.showSucessMessage(res.result)
            this.toQuery()
          }
        })
      })
    }
  }
}
</script>
